//
//  UIApplication+Links.m
//  Cachalot
//
//  Created by Adam Cue on 6/25/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CachalotApplication.h"

@implementation CachalotApplication

- (BOOL)openURL:(NSURL*)url {
   CachalotAppDelegate * ad = (CachalotAppDelegate *)[self delegate];
   UIViewController *toShow = [[ad.tabBar viewControllers] objectAtIndex:ad.tabBar.selectedIndex];
   if([[url scheme] hasPrefix:@"http"]){
      WebViewController * vc = [[[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil address:[url absoluteString]] autorelease];
      [toShow presentModalViewController:vc animated:YES];
      return NO;
   }
   else if([[url scheme] hasPrefix:@"mailto"]){
      MFMailComposeViewController * vc = [[MFMailComposeViewController alloc] init];
      [vc setToRecipients:[NSArray arrayWithObject:[url resourceSpecifier]]];
      [vc setMessageBody:CACHALOT_IMAGE_EMAIL_TEXT isHTML:YES];
      vc.mailComposeDelegate = self;
      [toShow presentModalViewController:vc animated:YES];
      return NO;
   }
   else if([[url scheme] hasPrefix:@"shttp"]){
      return [super openURL:[NSURL URLWithString:[url.absoluteString substringFromIndex:1]]];
   }
   return YES;
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
   [controller dismissModalViewControllerAnimated:YES];
}

@end
